package com.ruoyi.fin.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fin.domain.FinTransaction;
import com.ruoyi.fin.domain.FinAccount;
import com.ruoyi.fin.service.IFinTransactionService;
import com.ruoyi.fin.service.IFinAccountService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 财务交易记录Controller
 * 
 * @author ruoyi
 * @date 2025-06-19
 */
@Controller
@RequestMapping("/fin/transaction")
public class FinTransactionController extends BaseController
{
    private String prefix = "fin/transaction";

    @Autowired
    private IFinTransactionService finTransactionService;
    
    @Autowired
    private IFinAccountService finAccountService;

    @RequiresPermissions("fin:transaction:view")
    @GetMapping()
    public String transaction(ModelMap mmap, Long accountId)
    {
        // 如果未指定账户ID，使用默认账户
        if (accountId == null) {
            accountId = 1L; // 默认账户ID
        }
        
        // 获取指定账户
        FinAccount account = finAccountService.selectFinAccountById(accountId);
        if (account != null) {
            mmap.put("currentBalance", account.getBalance());
            mmap.put("accountName", account.getName());
            mmap.put("currentAccountId", accountId);
        }
        
        // 获取所有可用账户列表
        FinAccount queryAccount = new FinAccount();
        queryAccount.setStatus(1L); // 只查询启用状态的账户
        List<FinAccount> accounts = finAccountService.selectFinAccountList(queryAccount);
        mmap.put("accounts", accounts);
        
        return prefix + "/transaction";
    }

    /**
     * 查询财务交易记录列表
     */
    @RequiresPermissions("fin:transaction:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(FinTransaction finTransaction)
    {
        startPage();
        List<FinTransaction> list = finTransactionService.selectFinTransactionList(finTransaction);
        return getDataTable(list);
    }
    
    /**
     * 获取当前账户余额
     */
    @GetMapping("/currentBalance")
    @ResponseBody
    public AjaxResult getCurrentBalance(Long accountId)
    {
        if (accountId == null) {
            accountId = 1L; // 默认账户ID
        }
        
        FinAccount account = finAccountService.selectFinAccountById(accountId);
        if (account != null) {
            return AjaxResult.success("查询成功", account.getBalance());
        } else {
            return AjaxResult.error("账户不存在");
        }
    }

    /**
     * 导出财务交易记录列表
     */
    @RequiresPermissions("fin:transaction:export")
    @Log(title = "财务交易记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FinTransaction finTransaction)
    {
        List<FinTransaction> list = finTransactionService.selectFinTransactionList(finTransaction);
        ExcelUtil<FinTransaction> util = new ExcelUtil<FinTransaction>(FinTransaction.class);
        return util.exportExcel(list, "财务交易记录数据");
    }

    /**
     * 新增财务交易记录
     */
    @RequiresPermissions("fin:transaction:add")
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        // 获取所有可用账户列表供选择
        FinAccount queryAccount = new FinAccount();
        queryAccount.setStatus(1L); // 只查询启用状态的账户
        List<FinAccount> accounts = finAccountService.selectFinAccountList(queryAccount);
        mmap.put("accounts", accounts);
        
        return prefix + "/add";
    }

    /**
     * 新增保存财务交易记录
     */
    @RequiresPermissions("fin:transaction:add")
    @Log(title = "财务交易记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FinTransaction finTransaction)
    {
        return toAjax(finTransactionService.insertFinTransaction(finTransaction));
    }

    /**
     * 修改财务交易记录
     */
    @RequiresPermissions("fin:transaction:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        FinTransaction finTransaction = finTransactionService.selectFinTransactionById(id);
        mmap.put("finTransaction", finTransaction);
        
        // 获取所有可用账户列表供选择
        FinAccount queryAccount = new FinAccount();
        queryAccount.setStatus(1L); // 只查询启用状态的账户
        List<FinAccount> accounts = finAccountService.selectFinAccountList(queryAccount);
        mmap.put("accounts", accounts);
        
        return prefix + "/edit";
    }

    /**
     * 修改保存财务交易记录
     */
    @RequiresPermissions("fin:transaction:edit")
    @Log(title = "财务交易记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FinTransaction finTransaction)
    {
        return toAjax(finTransactionService.updateFinTransaction(finTransaction));
    }

    /**
     * 删除财务交易记录
     */
    @RequiresPermissions("fin:transaction:remove")
    @Log(title = "财务交易记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(finTransactionService.deleteFinTransactionByIds(ids));
    }
    
    /**
     * 从采购订单创建财务交易记录
     */
    @RequiresPermissions("fin:transaction:add")
    @Log(title = "采购交易记录", businessType = BusinessType.INSERT)
    @PostMapping("/createFromPurchase")
    @ResponseBody
    public AjaxResult createFromPurchase(Long purchaseOrderId, Long accountId)
    {
        return toAjax(finTransactionService.createTransactionFromPurchase(purchaseOrderId, accountId));
    }
    
    /**
     * 从销售订单创建财务交易记录
     */
    @RequiresPermissions("fin:transaction:add")
    @Log(title = "销售交易记录", businessType = BusinessType.INSERT)
    @PostMapping("/createFromSales")
    @ResponseBody
    public AjaxResult createFromSales(Long salesOrderId, Long accountId)
    {
        return toAjax(finTransactionService.createTransactionFromSales(salesOrderId, accountId));
    }
}
